/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.itip.event.send;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * <p>
 * Description: 参数初始化  log4j db ...
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-8-13 上午9:53:49          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class EnvUtil {

    private static final Logger logger = Logger.getLogger(EnvUtil.class);

    Properties prop = null;

    private static EnvUtil instance = null;

    private static Object _lock = new Object();

    private String cols = "";

    private String ip = "";

    private int port = 0;

    private String source = "";

    private String interval = "30";

    private String sql = "";

    private String columns = "";

    private String tpcnodes = "";

    Connection conn = null;

    public static synchronized EnvUtil getInstance() {
        if (instance == null) {
            synchronized (_lock) {
                instance = new EnvUtil();
            }
        }
        return instance;
    }

    /**
     * 
     */
    private EnvUtil() {
        initConf();
        //        initDb();
    }

    /**
     * 初始化配置参数配置
     */
    private void initConf() {
        try {
            PropertyConfigurator.configure("itip-conf/log4j.properties");
            //
            prop = getProperties("itip-conf/tivoli-event.properties");
            cols = prop.getProperty("alerts.status.cols", "");
            ip = prop.getProperty("probe.ip", "127.0.0.1");
            port = Integer.parseInt(prop.getProperty("probe.socket.port", "9900"));
            source = prop.getProperty("source", "Tivoli");

            sql = prop.getProperty("collect.event.sql");
            columns = prop.getProperty("columns");
            this.setInterval(prop.getProperty("collect.interval", "30"));

            tpcnodes = prop.getProperty("tpc.node");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 构建查询事件的脚本
     * @return
     */
    public String buildSql() {
        return this.sql;
    }

    /**
     * 初始化omnibus数据库配置
     */
    //    private void initDb() {
    //        ds = new BasicDataSource();
    //        ds.setDriverClassName(prop.getProperty("sybase.driver", "com.sybase.jdbc3.jdbc.SybDriver"));
    //        ds.setUrl(prop.getProperty("sybase.url", ""));
    //        ds.setUsername(prop.getProperty("sybase.user", "tivoli"));
    //        ds.setPassword(prop.getProperty("sybase.pwd", "tivoli"));
    //        ds.setMaxActive(3);
    //        ds.setMaxIdle(1);
    //        ds.setMinIdle(1);
    //        ds.setMaxWait(10 * 1000L);
    //        logger.info("Create new db2 connection ");
    //    }

    //    public Connection getConnection() {
    //        Connection conn = null;
    //        try {
    //            conn = ds.getConnection();
    //            if (conn == null) {
    //                throw new SQLException("no idle connection");
    //            }
    //        } catch (SQLException e) {
    //            logger.error(e);
    //        }
    //        return conn;
    //    }

    /**
     * 获取omnibus连接
     * @return
     */
    public Connection getConnection() {
        if (conn == null) {
            try {
                logger.info("[TIP]\tCreate A New Sysbase Connection");
                Class.forName(prop.getProperty("sybase.driver", "com.sybase.jdbc3.jdbc.SybDriver"));
                conn = DriverManager.getConnection(prop.getProperty("sybase.url", ""), prop.getProperty("sybase.user", "tivoli"), prop.getProperty("sybase.pwd", "tivoli"));
            } catch (Exception e) {
                logger.error(e);
            }
        }
        return conn;
    }

    /**
     * 当查询出异常时 将此conn置为NULL,这样下次就会重新创新一个连接
     */
    public void closeConn() {
        if (this.conn != null) {
            try {
                this.conn.close();
            } catch (Exception e) {
                logger.error(e);
            } finally {
                this.conn = null;
            }
        }
    }

    /**
     * 
     * @param filename
     * @return
     * @throws IOException
     * @throws FileNotFoundException
     */
    private Properties getProperties(String filename) throws IOException, FileNotFoundException {
        if (filename == null || filename.trim().length() == 0) {
            throw new FileNotFoundException("Files Name can't be null or String of length 0.");
        }
        FileInputStream configStream = null;
        Properties prop = new Properties();
        try {
            configStream = new FileInputStream(filename);
            prop.load(configStream);
        } finally {
            if (null != configStream) {
                try {
                    configStream.close();
                } catch (Throwable t) {
                    t.getMessage();
                }
            }
        }
        return prop;
    }

    public String getCols() {
        return cols;
    }

    public String getIp() {
        return ip;
    }

    public int getPort() {
        return port;
    }

    public String getSource() {
        return source;
    }

    public String getInterval() {
        return interval;
    }

    public void setInterval(String interval) {
        this.interval = interval;
    }

    public String getColumns() {
        return columns;
    }

    public String getTpcnodes() {
        return tpcnodes;
    }

}
